package it.amicosmanettone.archive;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

import android.content.Context;
import android.util.Log;


public class ArchiveManager {

	private static String FILENAME_ARCHIVE = "archive.dat";

	/**
	 * Write portfolio list array as java serialized object in a file
	 * 
	 * @param context
	 * @return
	 */
	public static String storeObject(Context context,String obj, String filename) {
		try {
			Log.v("ARCHIVE", "Provo a salvare nel file " + FILENAME_ARCHIVE);
			FileOutputStream fos = context.openFileOutput(filename,Context.MODE_PRIVATE);
			ObjectOutputStream os = new ObjectOutputStream(fos);
			os.writeObject(obj);
			os.close();
			Log.v("ARCHIVE", "File " + filename	+ " salvato correttamente");
		} catch (Exception e) {
			e.printStackTrace();
			return (ErrorCode.ERR_READ_SAVED_ARCHIVE);
		}// End try..catch...
		return (ErrorCode.NO_ERROR);
	}// End method...

	/**
	 * Retrieve ArrayList from serialized object in a file
	 * 
	 * @param context
	 * @return
	 */
	public static Object retrieveObject(Context context, String filename) {
		Object objRead = null;
		try {
			Log.v("ARCHIVE", "Provo a leggere dal file " + filename);
			//ArrayList<PortfolioBean> list = new ArrayList<PortfolioBean>();
			FileInputStream fis = context.openFileInput(FILENAME_ARCHIVE);
			ObjectInputStream is = new ObjectInputStream(fis);
			objRead = (Object) is.readObject();
			is.close();
			Log.v("ARCHIVE", "Recuperato file di archivio");
			return(objRead);
		} catch (Exception e) {
			e.printStackTrace();
		}// End try..catch...
		return (null);
	}// End method..

}// End class...
